package com.teaching.Dao;

import com.teaching.Enity.Teacher;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TeacherDaoAchieve implements TeacherDao {
    private SqlSession sqlSession;
    private String resource = "mybatis.xml";

    @Override//获取全部老师
    public List<Teacher> findAllTeacher() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        List<Teacher> teacherList = sqlSession.selectList("findAllTeacher");
        sqlSession.commit();
        sqlSession.close();
        return teacherList;
    }

    @Override//获取某一个老师的个人信息
    public List<Teacher> findTeacher(int id) throws IOException{
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        List<Teacher> teacherList = sqlSession.selectList("findTeacher",id);
        sqlSession.commit();
        sqlSession.close();
        return teacherList;
    }

    @Override//更新数据库中老师密码
    public void modifyTeacherPwd(Teacher teacher) throws IOException{
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        sqlSession.update("modifyTeacherPwd",teacher);
        sqlSession.commit();
        sqlSession.close();
    }
}
